using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 继承自MonoBehavior的单例模式
/// 全局单例
/// </summary>
/// <typeparam name="T">脚本</typeparam>
public class SingletonMono<T> :MonoBehaviour where T : MonoBehaviour
{
    private static T _instance;

    public static T Instance
    {
        get
        {
            if(_instance == null)
            {
                // 动态挂载，自动挂载
                GameObject obj = new GameObject(typeof(T).ToString() + " Global Singleton");

                _instance = obj.AddComponent<T>();

                DontDestroyOnLoad(obj);
            }

            return _instance;
        }
    }
}
